package com.mzy.basics.utils.Java;

/**
 * @author Ives
 * * @date 2022/1/12
 */
public class Random {

  public static void main(String[] args) {
    String generate = generate();
    System.out.println(generate);
    java.util.Random random = new java.util.Random();
    for (int i = 0; i < 10; i++) {
      int x = random.nextInt(2);
      System.out.println(x);
    }
  }

  /**
   * @return String
   * @description 隨機(數字 + 字母), 生成20位字符串
   */
  private static String generate() {
    java.util.Random random = new java.util.Random();
    String val = "";
    for (int i = 0; i < 20; i++) {
      String str = random.nextInt(2) % 2 == 0 ? "num" : "char";
      if ("char".equalsIgnoreCase(str)) { // 产生字母
        int nextInt = random.nextInt(2) % 2 == 0 ? 65 : 97;
        val += (char) (nextInt + random.nextInt(26));
      } else if ("num".equalsIgnoreCase(str)) { // 产生数字
        val += String.valueOf(random.nextInt(10));
      }
    }
    return val;
  }
}
